/*   THIS IS A MODIFIED VERSION OF MENU.JS SYSTEM SCRIPT   */

function CMenu(message,leave)
{
	//if (this instanceof CMenu == false) return new CMenu();
	this.msg = message;
	this.stay = leave;
	this.items = new Array();
	this.items2 = new Array();
}


CMenu.prototype.addItem = function(name, callback, what, color)
{
	if (what == undefined) what = "";
	var item = new Object;
	item.name     = name;
	item.callback = callback;
	item.color    = color;
	item.desc = what;
	this.items[this.items.length] = item;
}

CMenu.prototype.addItem2 = function(name, callback, what, color)
{
	if (what == undefined) what = "";
	var item = new Object;
	item.name     = name;
	item.callback = callback;
	item.color    = color;
	item.desc = what;
	this.items2[this.items2.length] = item;
}

CMenu.prototype.execute = function(x, y, w, h)
{
	var text_height = font.getHeight();
	var shown_items = Math.floor(h / text_height);
	var down_arrow = LoadAnimation("greenarrow.mng");
	var up_arrow;
	var lastframetime = GetTime();
	var selection = 0;
	var top_selection = 0;
	var column = 0;
		//put cursor at current sprite in spritetesting debug menu
	if (isdude("spritetester") && GetCurrentPerson()=="ghostofflesh")
	{
		for (var r=0; r<this.items.length; r++)
			if (this.items[r].name == spritetester.rss)
				selection = r;
		if (selection > shown_items) top_selection = selection;
		if (this.items.length-1 - top_selection < shown_items)
			top_selection = this.items.length-1 - shown_items;
	}
	var redraw = true;
	var screen;
	var finger = LoadImage("thefinger.png");
	
	ApplyColorMask(Black);
	window.drawWindow(xbo,ybo, 320-2*xbo, 35-2*ybo);
	//alphagrad.transformBlit(xbo, ybo-6,  320-xbo, ybo-6,   320-xbo, ybo+20,   xbo, ybo+20);
	font.drawText(160-font.getStringWidth(this.msg)/2, ybo, this.msg);
	window.drawWindow(x, y, w, h);
	alphagrad.blit(0,0);	//xbo, y-6,    319-xbo,y-6,    319-xbo, 236,  xbo, 236);
	var menyas = GrabImage(0,0,320,240);

	while (true) 
	{
		if (redraw)
		{
			redraw = false;
			menyas.blit(0,0);	//redrawing_windows = lag.6
			// draw the menu items
			for (var i=0; i<= shown_items; i++)
			{	//column 1
				if (i < this.items.length-top_selection)
				{					
					if (this.items[i+top_selection].color != undefined) font.setColorMask(this.items[i + top_selection].color);
					else font.setColorMask(White);
					font.drawText(x+16,	y+ i*text_height,	this.items[i+top_selection].name);
				}
				//column 2
				if (i < this.items2.length-top_selection)
				{
					if (this.items2[i+top_selection].color != undefined) font.setColorMask(this.items2[i + top_selection].color);
					else font.setColorMask(White);
					font.drawText(x+16+160,	y+ i*text_height,	this.items2[i+top_selection].name);
				}
			}
			screen = GrabImage(0,0,320,240);
		}
		
		font.setColorMask(White);
		if (lastframetime + down_arrow.getDelay() < GetTime())
		{ down_arrow.readNextFrame(); lastframetime = GetTime(); }
		down_arrow.drawFrame(160,200);	//up_arrow is just a mirror of down_arrow
		up_arrow = GrabSurface(160,200, 12, 8);
		up_arrow.flipVertically();
		screen.blit(0,0);	
			// draw the up and down arrows if necessary
		if (top_selection + shown_items < this.items.length-1)
			down_arrow.drawFrame(x + w - 15, y + text_height * shown_items);
		if (top_selection > 0)
			up_arrow.blit(x + w - 12, y-7);
		finger.blit(x+160*column, y + (selection - top_selection) * text_height);
		font.drawText(xbo*2, 38, ((column==0) ? this.items[selection].desc : this.items2[selection].desc) );
		
		FlipScreen();

		while (AreKeysLeft())
		{
		switch (GetKey())
		{
		
		case key_talk:
		{
			if (column==0)
				eval(this.items[selection].callback);
			else
				eval(this.items2[selection].callback);
			if (this.stay) break;
			else return;
		}
	
		case KEY_ESCAPE: return;
		case key_cancel:	return;
	
		case KEY_DOWN:
		{
			if (selection < ((column==0) ? this.items.length-1 : this.items2.length-1))
			{
				selection++;
				if (selection > top_selection + shown_items)
				{ top_selection++; redraw = true; }
			}
			break;
		}
	
		case KEY_UP:
		{
			if (selection > 0)
			{
				selection--;
				if (selection < top_selection)
				{ top_selection--; redraw = true; }
			}
			break;
		}

		case KEY_RIGHT:
		{
			if (column == 0 && this.items2.length>0)
			{
				if (this.items2.length-1 < selection)
				{
					if (top_selection > this.items2.length-1)
					{
						top_selection = this.items2.length-1 - shown_items;
						if (top_selection < 0) top_selection = 0;
					}
					selection = this.items2.length-1;
					redraw = true;
				}
				column = 1;
			}
			break;
		}
		
		case KEY_LEFT:
		{
			if (column == 1 && this.items.length>0)
			{
				if (this.items.length-1 < selection)
				{
					if (top_selection > this.items.length-1)
					{
						top_selection = this.items.length-1 - shown_items;
						if (top_selection < 0) top_selection = 0;
					}
					selection = this.items.length-1;
					redraw = true;
				}
				column = 0;
			}
			break;
		}
				
		case KEY_PAGEUP:
		{
			if ( ((column==0) ? this.items.length-1 : this.items2.length-1) > shown_items)
			{
				if (top_selection - (shown_items+1) >= 0)
				{
					top_selection -= shown_items+1;
					selection -= shown_items+1;
				}
				else
				{
					top_selection = 0;
					if (selection - shown_items >= 0) selection -= shown_items;
					else selection = 0;
				}
				redraw = true;
			}
			break;
		}
		
		case KEY_PAGEDOWN:
		{
			var thelength = (column==0) ? this.items.length-1 : this.items2.length-1;
			
			if (thelength > shown_items)
			{
				if (thelength - (top_selection+shown_items+1) >= shown_items)	//if there's room for a full pagedown
				{
					top_selection += shown_items+1;
					selection += shown_items+1;
				}
				else
				{
					top_selection = thelength - shown_items;
					if ((selection + shown_items) < thelength)
						selection += shown_items;
					else selection = thelength;
				}
				redraw = true;
			}
			break;
		}

		case KEY_HOME:
		{
			selection = 0;
			top_selection = 0;
			redraw = true;
			break;
		}
		
		case KEY_END:
		{
			var thelength = (column==0) ? this.items.length-1 : this.items2.length-1;
			
			selection = thelength;
			if (thelength > shown_items) top_selection = thelength - shown_items;
			redraw = true;
			break;
		}
	
		}
		} // end handle input
	}
}
